﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraReports.UserDesigner;
using System.IO;
using Ahi.Base;
using Ahi.Controller;
using Ahi.Config;
using Ahi.Entity;
using System.Windows.Forms;
using MongoDB.Driver.Builders;



namespace Ahi.Raporlama
{
    public class OpenCommandHandler : ICommandHandler
    {
        private XRDesignPanel panel;
        private IdFormParameter idFormParameter;

        public OpenCommandHandler(XRDesignPanel xrDesignPanel1, IdFormParameter idFormParameter)
        {
            this.panel = xrDesignPanel1;
            this.idFormParameter = idFormParameter;
        }

        public bool CanHandleCommand(ReportCommand command)
        {
            return command == ReportCommand.OpenFile ||
                command == ReportCommand.NewReport;
        }

        public void HandleCommand(ReportCommand command, object[] args, ref bool handled)
        {
            RaporEntity entity = null;

            if (CanHandleCommand(command))
            {
                DevExpress.XtraReports.UI.XtraReport rpt = new DevExpress.XtraReports.UI.XtraReport();
                if (idFormParameter.HasValue)
                {
                    entity = MongoAdaptor.Instance.GetDataById<RaporEntity>(idFormParameter.AsObjectId);

                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(entity.KaynakKod)))
                    {
                        rpt.LoadLayout(ms);
                    }
                }

                bool hasTopMargin = false;
                bool hasDetail = false;
                bool hasBottomMargin = false;

                foreach (DevExpress.XtraReports.UI.Band item in rpt.Bands)
                {
                    if (!hasTopMargin)
                        hasTopMargin = item is DevExpress.XtraReports.UI.TopMarginBand;
                    if (!hasDetail)
                        hasDetail = item is DevExpress.XtraReports.UI.DetailBand;
                    if (!hasBottomMargin)
                        hasBottomMargin = item is DevExpress.XtraReports.UI.BottomMarginBand;
                }
                if (!hasTopMargin)
                    rpt.Bands.Add(new DevExpress.XtraReports.UI.TopMarginBand());
                if (!hasDetail)
                    rpt.Bands.Add(new DevExpress.XtraReports.UI.DetailBand());
                if (!hasBottomMargin)
                    rpt.Bands.Add(new DevExpress.XtraReports.UI.BottomMarginBand());

                panel.OpenReport(rpt);
                if (idFormParameter.HasValue)
                {
                    BindingSource bs = new BindingSource();
                    bs.DataSource = entity.DataSourceType;
                    panel.Report.Container.Add(bs, "bindingSource1");
                    panel.Report.DataSource = bs;
                }
                panel.ReportState = ReportState.Opened;
                handled = true;
            }
        }
    }
}
